
********************************************************************************	
cd $pathdata_processed
use terminales_fin, clear

sum chance
replace chance = chance/`r(sd)'
replace expected_gpa = expected_gpa/`r(sd)'
replace predicted_gpa = predicted_gpa/`r(sd)'

rename stdteachers_grades teach
rename stdteachers_grades2 teach2
********************************************************************************	    
global controls teach teach2 stdms_gpa stdms_gpa2 m_ms_gpa ///
		 female ///
		 age age2 ///
		 p_age p_age2 m_mage m_fage ///
		 p_educ p_educ2 m_mbu_kl m_fbu_kl ///
		 ln_p_inc m_mwyrkinnt_c m_fwyrkinnt_c 		
		 
global controls2 stdms_gpa stdms_gpa2 m_ms_gpa ///
		 female ///
		 age age2 ///
		 p_age p_age2 m_mage m_fage ///
		 p_educ p_educ2 m_mbu_kl m_fbu_kl ///
		 ln_p_inc m_mwyrkinnt_c m_fwyrkinnt_c					 
************************************************************************
global partial_gpa i.year_vg3 i.skole_fo

gen all = 1
********************************************************************************
cd $pathtable						
*******************************************************************************	
label var chance "$widetilde{\textrm{GPA_luck}}$"
label var predicted_gpa "$\textrm{GPA_luck}$"

label var stdexam_grades "Exam grades in 3\textsuperscript{rd} year"
label var stdvide3 "High school GPA"
label var completion "On time HS diploma"
label var hs_gs_c "Ever HS diploma"
label var he_e "Ever higher education"
label var share_available "Share of available HE programs"
label var rank_f_he "Selectivity of HE enrollment"
label var bu_sup "Number of completed years in HE"
label var first_job "Ever employed"
label var ln_wyrkinnt_c1 "First job labor income"
label var job10 "Employed 8 years after the exams"
label var ln_wyrkinnt_c10 "Labor income 8 years after the exams"
********************************************************************************
//IV
egen stdvide3q = xtile(stdvide3), nq(10)

*Labor market outcomes 
ivregress 2sls ln_wyrkinnt_c10 (stdvide3 = chance) $controls2 $partial_gpa if all == 1, cluster(group) rob
reg ln_wyrkinnt_c10 stdvide3 $controls2 $partial_gpa if all == 1, cluster(group) rob 

ivolsdec ln_wyrkinnt_c10 (stdvide3=chance) $controls2 $partial_gpa, xnb(i.stdvide3q) vce(cluster group) format(%7.3f)

matrix A = J(2,6,.)
matrix A[1,1] = r(D)[1,1]
matrix A[2,1] = r(D)[1,2]

matrix A[1,2] = r(D)[2,1]
matrix A[2,2] = r(D)[2,2]

matrix A[1,3] = r(D)[3,1]
matrix A[2,3] = r(D)[3,2]

matrix A[1,4] = r(D)[4,1]
matrix A[2,4] = r(D)[4,2]

matrix A[1,5] = r(D)[5,1]
matrix A[2,5] = r(D)[5,2]

matrix A[1,6] = r(D)[6,1]
matrix A[2,6] = r(D)[6,2]

clear
svmat A, names(col)

foreach var in c1 c2 c3 c4 c5 c6 {
	gen `var'2 = string(`var', "%6.3f")
	drop `var'
	rename `var'2 `var'
	replace `var' = "(" + `var' + ")" if _n == 2
}

texsave using decom_base.tex, replace frag

preserve
clear
set obs 1
local tex "decom_base.tex"
local tex2 "decom_base.tex"

generate strL s = fileread("`tex'") if fileexists("`tex'")
assert filereaderror(s)==0
replace s = subinstr(s,"\begin{table}[tbp] \centering","",1)
replace s = subinstr(s,"\newcolumntype{R}{>{\raggedleft\arraybackslash}X}","",1)
replace s = subinstr(s,"\newcolumntype{L}{>{\raggedright\arraybackslash}X}","",1)
replace s = subinstr(s,"\newcolumntype{C}{>{\centering\arraybackslash}X}","",1)
replace s = subinstr(s,"\begin{tabularx}{\linewidth}{@{}lCCCCC@{}}","",1)
replace s = subinstr(s,"\begin{tabularx}{\linewidth}{lCCCCC}","",1)
replace s = subinstr(s,"\toprule","",1)
replace s = subinstr(s,"{c1}&{c2}&{c3}&{c4}&{c5}&{c6}","",1)
replace s = subinstr(s,"\tabularnewline","",1)
replace s = subinstr(s,"\addlinespace[\belowrulesep]","",1)
replace s = subinstr(s,"\tabularnewline","\\",2)
replace s = subinstr(s,"\end{tabularx}","",9)
replace s = subinstr(s,"\end{table}","",9)

gen byte fw = filewrite("`tex2'",s,1)
restore	

